﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core;
using System.Linq.Expressions;
using MShop.Core.Domain;
using MShop.Web.Components.Paging;
using MShop.Core.Extensions;
using Castle.Core;
using MShop.Web.Components.ModelBinders;
using MShop.Web.Components.ValueProvider;
namespace MShop.Web.Areas.Admin.Controllers
{

    public class ProductAdController : SecureController
    {
        private readonly IProductDao _ProductDao;
        private readonly IItemCatDao _ItemCatDao;
        private readonly ISupplierDao _SupplierDao;
        private readonly IBrandDao _BrandDao;
        private readonly ICommonDao<ProductProp> _CommonDao;
        private readonly ICommonDao<ProductImg> _CommonDao2;
        private readonly IDictDao _DictDao;
        public ProductAdController(IProductDao productDao, IItemCatDao itemcatdao, IBrandDao brandDao, 
            ISupplierDao supplierDao, ICommonDao<ProductProp> commonDao,ICommonDao<ProductImg> commonDao2,IDictDao dictDao)
        {
           // System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture;
            _ItemCatDao = itemcatdao;
            _ProductDao = productDao;
            _BrandDao = brandDao;
            _SupplierDao = supplierDao;
            _CommonDao = commonDao;
            _CommonDao2 = commonDao2;
            _DictDao = dictDao;
        }

 

        public ActionResult Index()
        {
            if (Session["productids"] == null)
            {
                return View(Enumerable.Empty<Product>());
            }
            else
            {
                List<int> ids = Session["productids"] as List<int>;
                var q = from item in _ProductDao.GetValidProducts() where ids.Contains(item.Id) select item;
                return View(q);
            }
        }

        [HttpPost]
        public ActionResult ShowProductAd(FormCollection form)
        {
            if (Session["productids"] == null)
            {
                return View(Enumerable.Empty<Product>());
            }
            else
            {
                List<int> ids = Session["productids"] as List<int>;
                var q = from item in _ProductDao.GetValidProducts() where ids.Contains(item.Id) select item;
                
                return View(q);
            }
        }

        public ActionResult Delete(int id)
        {
            if (Session["productids"] != null)
            {
                List<int> ids = Session["productids"] as List<int>;
                ids.Remove(id);
                Session["productids"] = ids;
            }
            return RedirectToAction("index");
        }

        public ActionResult AddId2Session(int id)
        {
            if (Session["productids"] == null)
            {
                List<int> ids = new List<int>();
                ids.Add(id);
                Session["productids"] = ids;
            }
            else
            {
                List<int> ids = Session["productids"] as List<int>;
                if (!ids.Contains(id))
                {
                    ids.Add(id);
                    Session["productids"] = ids;
                }
                else
                {
                    return Content("2");
                }
            }
            return Content("1");
        }

      
    }
}
